package com.example.hao.flowercityfm.utils;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import okhttp3.Interceptor;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;

public class RetrofitClient {


    private static final class Type {
        static final MediaType TYPE_FILE = MediaType.parse("application/octet-stream");
    }


    public static final String BASE_URL = "https://api.myjson.com/bins/12iuew";
    private Retrofit retrofit;
    private OkHttpClient okHttpClient;


    private APIRequestService apiRequestService;


    /**
     * 连接超时时间
     */
    private int CONNECT_TIMEOUT = 15;
    /**
     * 读取超时时间
     */
    private int READ_TIMEOUT = 5;
    /**
     * 写出数据超时时间
     */
    private int WRITE_TIMEOUT = 5;

    public static RetrofitClient getClient() {
        return Singleton.CLIENT;
    }

    private static final class Singleton {
        static final RetrofitClient CLIENT = new RetrofitClient();
    }

    public RetrofitClient upDateUrl(String baseUrl) {
        okHttpClient = new OkHttpClient.Builder()
                .connectTimeout(CONNECT_TIMEOUT, TimeUnit.SECONDS)
                .readTimeout(READ_TIMEOUT, TimeUnit.SECONDS)
                .writeTimeout(WRITE_TIMEOUT, TimeUnit.SECONDS)
                //配置 OkHTTP的拦截器
//                .addInterceptor()
                //配置 cookie 缓存, cookie 其实就是一个有规律的()字符串
//                .cookieJar()
                .build();


        retrofit = new Retrofit.Builder()
                //配置OKHTTPClient
                .client(okHttpClient)
                //基础服务器地址
                .baseUrl(baseUrl)
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                //GSon 转换流
                .addConverterFactory(GsonConverterFactory.create())
                //字符转换流
                .addConverterFactory(ScalarsConverterFactory.create())
                .build();

        apiRequestService = retrofit.create(APIRequestService.class);
        return this;
    }

    public static APIRequestService getApiRequestService() {
        return getClient().apiRequestService;
    }


    private RetrofitClient() {

        okHttpClient = new OkHttpClient.Builder()
                .connectTimeout(CONNECT_TIMEOUT, TimeUnit.SECONDS)
                .readTimeout(READ_TIMEOUT, TimeUnit.SECONDS)
                .writeTimeout(WRITE_TIMEOUT, TimeUnit.SECONDS)
                //配置 OkHTTP的拦截器
                .addInterceptor(new Interceptor() {
                    @Override
                    public Response intercept(Chain chain) throws IOException {
                        Request request = chain.request();
                        Response response = chain.proceed(request);
                        return response;
                    }
                })
                //配置 cookie 缓存, cookie 其实就是一个有规律的()字符串
//                .cookieJar()
                .build();

        retrofit = new Retrofit.Builder()
                //配置OKHTTPClient
                .client(okHttpClient)
                //基础服务器地址
                .baseUrl(BASE_URL)
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                //GSon 转换流
                .addConverterFactory(GsonConverterFactory.create())
                //字符转换流
                .addConverterFactory(ScalarsConverterFactory.create())
                .build();

        apiRequestService = retrofit.create(APIRequestService.class);

    }


}
